Imports System.Web.HttpContext


Namespace Workflow


    Public Class CUtilities

#Region "Event Scripts - Web controls"
        Public Shared Sub OnReturnPress(ByVal txt As TextBox, ByVal jscript As String)
            OnEvent(txt, "onkeypress", "if (13 == event.keyCode) {" & jscript & "; return false;}")
        End Sub
        Public Shared Sub OnReturnPress(ByVal txt As TextBox, ByVal btn As Button)
            OnReturnPress(txt, DhtmlId(btn) & ".click();")
        End Sub
        Public Shared Sub OnReturnPress(ByVal txt As TextBox, ByVal btn As ImageButton)
            OnReturnPress(txt, DhtmlId(btn) & ".click();")
        End Sub
        Public Shared Sub OnClick(ByVal ctrl As WebControl, ByVal jscript As String)
            OnEvent(ctrl, "onclick", jscript)
        End Sub
        Public Shared Sub ConfirmClick(ByVal ctrl As WebControl, ByVal msg As String)
            OnClick(ctrl, "return confirm('" & msg.Replace("'", "\'") & "')")
        End Sub
        Public Shared Sub OnEvent(ByVal ctrl As WebControl, ByVal eventName As String, ByVal jscript As String)
            ctrl.Attributes.Add(eventName, jscript)
        End Sub


        Public Shared Sub OnKeyUp(ByRef target As WebControl, Optional ByVal script As String = "return ValidateNumber()")
            OnEvent(target, "onkeyup", script)
        End Sub
        Public Shared Sub OnKeyPress(ByRef target As WebControl, Optional ByVal script As String = "return ValidateNumber()")
            OnEvent(target, "onkeypress", script)
        End Sub
        Public Shared Sub OnDblClick(ByRef target As WebControl, ByVal script As String)
            target.Attributes.Add("ondblclick", script)
        End Sub
        Public Shared Sub OnDblClick(ByRef target As WebControl, ByRef clickMe As WebControl)
            target.Attributes.Add("ondblclick", clickMe.UniqueID & ".click()")
        End Sub
#End Region


#Region "Event Scripts - Html controls"
        Public Shared Sub OnClick(ByVal ctrl As HtmlControl, ByVal jscript As String)
            OnEvent(ctrl, "onclick", jscript)
        End Sub
        Public Shared Sub ConfirmClick(ByVal ctrl As HtmlControl, ByVal msg As String)
            OnClick(ctrl, "return confirm('" & msg.Replace("'", "\'") & "')")
        End Sub
        Public Shared Sub OnEvent(ByVal ctrl As HtmlControl, ByVal eventName As String, ByVal jscript As String)
            ctrl.Attributes.Add(eventName, jscript)
        End Sub
#End Region

#Region "Script - Page based"
        Public Shared Sub ScriptRefreshParent()
            Script("try { window.opener.document.location = window.opener.document.location + ' '; } catch(e){}")
        End Sub
        Public Shared Sub ScriptRedirectParent(ByVal url As String)
            Script("try { window.opener.document.location = '" & url.Replace("'", "\'") & "'; } catch(e){}")
        End Sub
        Public Shared Sub ScriptRedirect(ByVal url As String)
            Script("try { document.location = '" & url.Replace("'", "\'") & "'; } catch(e){}")
        End Sub
        Public Shared Sub ScriptRefreshParentAndClose()
            ScriptRefreshParent()
            ScriptCloseWindow()
        End Sub
        Public Shared Sub ScriptPopupAndClose(ByVal msg As String)
            ScriptPopup(msg)
            ScriptCloseWindow()
        End Sub
        Public Shared Sub ScriptCloseWindow()
            Script("window.close()")
            Current.Response.End()
        End Sub
        Public Shared Sub ScriptPopup(ByVal page As UI.Page, ByVal msg As String)
            Script(page, ScriptPopup(msg, False))
        End Sub
        Public Shared Sub Script(ByVal page As UI.Page, ByVal jscript As String)
            page.ClientScript.RegisterStartupScript(jscript.GetType(), Guid.NewGuid.ToString, jscript)
        End Sub
#End Region

#Region "Script - string based"
        Public Shared Function ScriptPopup(ByVal msg As String, Optional ByVal write As Boolean = True) As String
            Return Script("alert('" & msg.Replace("'", "\'") & "')", write)
        End Function
        Public Shared Function Script(ByVal jscript As String, Optional ByVal write As Boolean = True) As String
            Script = "<script>" & jscript & "</script>"
            If write Then Current.Response.Write(Script)
        End Function
        Public Shared Function DhtmlId(ByVal ctrl As WebControl, Optional ByVal includeDocumentDotAll As Boolean = True) As String
            DhtmlId = ctrl.UniqueID.Replace(":", "_")
            If includeDocumentDotAll Then
                Return "document.all['" & DhtmlId & "']"
            End If
        End Function
        Public Shared Function DhtmlId(ByVal ctrl As HtmlControl, Optional ByVal includeDocumentDotAll As Boolean = True) As String
            DhtmlId = ctrl.UniqueID.Replace(":", "_")
            If includeDocumentDotAll Then
                Return "document.all['" & DhtmlId & "']"
            End If
        End Function
#End Region

#Region "Dropdown"
        Public Shared Sub SetValue(ByVal dd As DropDownList, ByVal value As String)
            value = LCase(value)
            Dim i As Integer
            For i = 0 To dd.Items.Count - 1
                If LCase(dd.Items(i).Value) = value Then
                    dd.SelectedIndex = i
                    Exit Sub
                End If
            Next
        End Sub
#End Region

#Region "Textbox"
        Public Shared Sub RightAlign(ByRef txt As TextBox)
            txt.Style.Add("text-align", "right")
        End Sub
        Public Shared Sub ValidateNumber(ByRef txt As TextBox)
            RightAlign(txt)
            txt.MaxLength = 16
            OnEvent(txt, "onfocus", "select()")
            OnKeyPress(txt)
        End Sub
        Public Shared Sub ValidateInteger(ByRef txt As TextBox)
            RightAlign(txt)
            txt.MaxLength = 12
            OnEvent(txt, "onfocus", "select()")
            OnKeyPress(txt, "return ValidateInteger()")
        End Sub
        Public Shared Sub ForceUpperCase(ByRef txt As TextBox)
            OnKeyUp(txt, "ForceUpperCase(this)")
        End Sub
        Public Shared Sub SetMoney(ByRef txt As TextBox, ByVal amount As Decimal)
            txt.Text = Format(amount, "C")
        End Sub
        Public Shared Function GetMoney(ByRef txt As TextBox) As Decimal
            Return GetMoney(txt.Text)
        End Function
        Public Shared Function GetMoney(ByRef s As String) As Decimal
            s = Replace(s, "$", "")
            Try
                Return CDec(s)
            Catch
                Return 0
            End Try
        End Function
        Public Shared Function GetInteger(ByRef txt As TextBox) As Integer
            Return GetInteger(txt.Text)
        End Function
        Public Shared Function GetInteger(ByRef txt As String) As Integer
            Try
                Return CInt(txt)
            Catch
                Return 0
            End Try
        End Function
#End Region

#Region "DHTML Events"

#Region "Common Overloads"
#End Region

#End Region

    End Class

End Namespace
